package avicit.bdp.dms.tdm.service.adapter;

import avicit.bdp.common.utils.CheckParaUtils;
import avicit.bdp.common.utils.uploads.FileAdapterUtils;
import avicit.bdp.common.utils.uploads.IFileAdapter;
import avicit.bdp.dms.tdm.dto.BdpAssetMountFileDTO;
import avicit.bdp.dms.tdm.service.BdpAssetMountFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class FileAdapter {

  private static final Logger logger = LoggerFactory.getLogger(FileAdapter.class);

  @Autowired
  private BdpAssetMountFileService assetMountFileService;

  public void downloadFile(String assetId, String filePath, HttpServletRequest request, HttpServletResponse response) throws Exception {
    BdpAssetMountFileDTO assetFile = assetMountFileService.queryByAssetId(assetId);
    CheckParaUtils.checkDbObjectValidity(assetFile, logger, String.format("没有查到数据，请确保assetId:%s的文档是否存在", assetId));

    String projectId = FileAdapterUtils.getProjectIdFromPath(filePath);

    IFileAdapter fileAdapter = FileAdapterUtils.getFileAdapterByProject(projectId);
    fileAdapter.downloadFile(filePath, request, response);
  }

}
